package vn.axonactive.vision.testhelper;

import java.util.List;

import org.junit.runner.Description;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;

import ch.ivyteam.ivy.environment.Ivy;

public class ExecutionListener extends RunListener
{
	/**
	 * Called before any tests have been run.
	 * */
	public void testRunStarted(Description description)	throws java.lang.Exception
	{
		Ivy.log().info("Number of testcases to execute : " + description.testCount());
	}

	/**
	 *  Called when all tests have finished
	 * */
	public void testRunFinished(Result result) throws java.lang.Exception
	{
		Ivy.log().info("---------------------------------------------------");
		Ivy.log().info("Number of testcases executed : " + result.getRunCount());
		Ivy.log().info("Number of testcases ignored : " + result.getIgnoreCount());
		List<Failure> failureList= result.getFailures();
		
		for (Failure failure : failureList) {
			Ivy.log().info("Failure message: " + failure.getMessage() );
		}
	}

	/**
	 *  Called when an atomic test is about to be started.
	 * */
	public void testStarted(Description description) throws java.lang.Exception
	{
		Ivy.log().info("*Starting test case : "+ description.getMethodName());
	}

	/**
	 *  Called when an atomic test has finished, whether the test succeeds or fails.
	 * */
	public void testFinished(Description description) throws java.lang.Exception
	{
		Ivy.log().info("*Finished test case : "+ description.getMethodName());
	}

	/**
	 *  Called when an atomic test fails.
	 * */
	public void testFailure(Failure failure) throws java.lang.Exception
	{
		Ivy.log().info("Execution of test case failed : "+ failure.getTrace());
	}

	/**
	 *  Called when a test will not be run, generally because a test method is annotated with Ignore.
	 * */
	public void testIgnored(Description description) throws java.lang.Exception
	{
		Ivy.log().info("Execution of test case ignored : "+ description.getMethodName());
	}
}
